﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using SalePartial.Utility.WebComponents;
using SalePartial.Utility;
using SalePartial.BLL;
using XunPan.Web;

public partial class LoginControl : System.Web.UI.UserControl
{
    protected void login_ctl_LoggedIn(object sender, System.EventArgs e)
	{
		//if (Page.User.Identity.IsAuthenticated)//判断是否通过认证
		//{
		//string[] selectedUsersRoles = Roles.GetRolesForUser(Page.User.Identity.Name);

        string[] UsersToRoles = Roles.GetRolesForUser(login_ctl.UserName);
		//获取登录用户所属角色
		string tmpstr = "";
		string[] tmparray = null;
		string strsplit = ";";
		// 分隔用字符串
		tmpstr = SecurityUtility.Check_ID(UsersToRoles);

		tmparray = tmpstr.Split(strsplit.ToCharArray(), StringSplitOptions.None);
		Session["mg_power"] = tmparray;

        Session["UserName"] = login_ctl.UserName;


        MembershipUser CurrentUser = Membership.GetUser(login_ctl.UserName.ToString());
		Session["UserID"] = (Guid)CurrentUser.ProviderUserKey;

        bool result = UserBLL.StoreUserInfoToSession(login_ctl.UserName.ToString());


		string target = Request["url"];

		if (string.IsNullOrEmpty(target)) {
			//用户原请求页面为空时
            login_ctl.DestinationPageUrl = GlobalSetting.UrlHomePage;

		} else {
			//Response.Redirect("~/roles/ManagementWizard.aspx")
			//用户原请求页面不为空时，转到请求页面
			Response.Redirect(target, true);
		}

	}

    protected void login_ctl_LoginError(object sender, System.EventArgs e)
	{
        login_ctl.FailureText = "您输入的账户名和密码不匹配，请重新输入。";
        string us = login_ctl.UserName;
        string pwd = login_ctl.Password;

		if (Membership.ValidateUser(us, pwd)) {
		//Trace.Warn("ok")
		} else {
			//Dim vendor As MembershipUser = Membership.GetUser(us)
			//Dim tpwd As String

			//tpwd = vendor.ResetPassword()
			//Trace.Warn(tpwd)
		}
	}

	protected void Page_Load(object sender, System.EventArgs e)
	{
		if (Page.User.Identity.IsAuthenticated) {
			Response.Redirect(GlobalSetting.UrlHomePage, true);


		}
	}

    public LoginControl()
    {
        Load += Page_Load;
    }
}
